package cn.tannn.surenessdemo.user.dao;


import cn.tannn.surenessdemo.user.entity.AuthRoute;
import cn.tannn.surenessdemo.user.entity.AuthRouteResourceBind;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 前端路由-接口资源
 * @author tan
 * @date 2022-07-18 11:00:57
 */
public interface AuthRouteResourceBindDao extends JpaRepository<AuthRouteResourceBind, Long> {


    /**
     * 删除  前端路由-接口资源
     * @param routeCode AuthRoute.code
     */
    @Transactional(rollbackFor = Exception.class)
    @Modifying
    void deleteByRouteCode(String routeCode);

    /**
     * 删除 前端路由-接口资源
     *  AuthRoute.code
     * @param routeCodes AuthRoute.code
     */
    @Transactional(rollbackFor = Exception.class)
    @Modifying
    void deleteByRouteCodeIn(List<String> routeCodes);

    /**
     * 获取资源所属的路由
     * @param resourceCodes resourceCodes
     * @return AuthRoute
     */
    @Query("select arrb from AuthRouteResourceBind arrb, AuthRoute  ar " +
            "where arrb.resourceCode = ar.code and arrb.resourceCode in (:resourceCodes)")
    List<AuthRoute> findRouteBindResourceList(@Param("resourceCodes") List<String> resourceCodes);


    /**
     * 获取资源所属的路由
     * @param resourceCode resourceCode
     * @return AuthRoute
     */
    @Query("select arrb from AuthRouteResourceBind arrb, AuthRoute  ar " +
            "where arrb.resourceCode = ar.code and arrb.resourceCode = :resourceCode")
    List<AuthRoute> findRouteBindResourceList(@Param("resourceCode") String resourceCode);




    /**
     * 根据路由查询资源
     * @param routeCode routeCode
     * @return resourceCode
     */
    @Query("select arr.resourceCode from AuthRouteResourceBind arr " +
            "where arr.routeCode = :routeCode ")
    List<String> findResourceByRouteCode(@Param("routeCode") String routeCode);


    /**
     * 删除 资源 - 路由
     * @param routeCode 路由 code
     * @param resourceCode 资源 code
     */
    @Modifying
    @Transactional(rollbackFor = Exception.class)
    void deleteByRouteCodeAndResourceCode(String routeCode, String resourceCode);

}
